package de.tudarmstadt.ukp.muip.fibqdojo.rule;

import de.tudarmstadt.ukp.muip.fibqdojo.interfaces.IFIBQRule;
import de.tudarmstadt.ukp.muip.fibqdojo.interfaces.IFIBQSentence;
import de.tudarmstadt.ukp.muip.fibqdojo.interfaces.IFIBQSentenceToken;
import de.tudarmstadt.ukp.muip.fibqdojo.util.FIBQUtil;
import de.tudarmstadt.ukp.muip.fibqdojo.visitor.GetFormVisitor;

/**
 * Returns <code>true</code> if a token in a sentence is encountered with the
 * same provided form. The tokens comparison is case-sensitive.
 * 
 * @author Tobias Horsmann
 */
public class FormMatchingRule implements IFIBQRule {

    private final String form;

    public FormMatchingRule(String form) {
        this.form = form;
    }

    @Override
    public boolean match(IFIBQSentence sentence) {
        for (IFIBQSentenceToken token : FIBQUtil.getSentenceToken(sentence)) {
            boolean match = match(token);
            if (match) {
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean match(IFIBQSentenceToken sentenceToken) {
        GetFormVisitor formVisitor = new GetFormVisitor();
        sentenceToken.accept(formVisitor);
        String tokenForm = formVisitor.getForm();

        if (form.equals(tokenForm)) {
            return true;
        }

        return false;
    }

}
